package com.quectel.core.module.banner.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.core.NoticeConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.banner.dao.BannerDao;
import com.quectel.core.module.banner.dao.MobileBannerVillageDao;
import com.quectel.core.module.banner.dto.BannerDto;
import com.quectel.core.module.banner.entity.BannerEntity;
import com.quectel.core.module.banner.entity.MobileBannerVillageEntity;
import com.quectel.core.module.banner.service.BannerService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-12-16 09:11:29
 */
@DubboService
public class BannerServiceImpl implements BannerService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BannerServiceImpl.class);

    private static final String MOBILE_BANNER_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "MOBILE_BANNER_CACHE_BY_ID:";

    @Autowired
    private BannerDao bannerDao;
    @Autowired
    private MobileBannerVillageDao mobileBannerVillageDao;


    /**
     * 保存当前对象和相关联的小区
     *
     * @param bannerDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(BannerDto bannerDto) {
        if (bannerDto.getId() == null) {
            Long id = save(bannerDto);
            bannerDto.setId(id);
        } else {
            updateById(bannerDto);
        }

        //删除关联关系
        mobileBannerVillageDao.delete(new LambdaQueryWrapper<MobileBannerVillageEntity>()
                .eq(MobileBannerVillageEntity::getMobileBannerId, bannerDto.getId()));

        if (NoticeConstants.BINDING_VILLAGE_PART.equals(bannerDto.getIsBindingAll())) {
            //部分关联
            if (CollectionUtils.isNotEmpty(bannerDto.getVillageDtoList())) {
                for (VillageDto villageDto : bannerDto.getVillageDtoList()) {
                    MobileBannerVillageEntity mobileBannerVillageEntity = new MobileBannerVillageEntity();
                    mobileBannerVillageEntity.setId(Snowflake.nextId());
                    mobileBannerVillageEntity.setMobileBannerId(bannerDto.getId());
                    mobileBannerVillageEntity.setVillageId(villageDto.getId());
                    mobileBannerVillageDao.insert(mobileBannerVillageEntity);
                }
            }

        }
    }

    @Override
    public BannerDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                MOBILE_BANNER_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public BannerDto selectById(Long id) {
        BannerEntity entity = bannerDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, BannerDto.class));
    }

    @Override
    public List<BannerDto> queryList(Map<String, Object> params) {
        List<BannerEntity> list = bannerDao.queryList(params);
        List<BannerDto> result = CopyUtils.copyList(list, BannerDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private BannerDto paddingField(BannerDto dto) {
        if (dto != null) {
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return bannerDao.queryTotal(params);
    }

    @Override
    public Long save(BannerDto dto) {
        BannerEntity entity = CopyUtils.copyObj(dto, BannerEntity.class);
        entity.setId(Snowflake.nextId());
        bannerDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(BannerDto dto) {
        BannerEntity entity = CopyUtils.copyObj(dto, BannerEntity.class);
        bannerDao.updateById(entity);
        CacheKit.invalidRedisCache(MOBILE_BANNER_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(BannerDto dto) {
        BannerEntity entity = CopyUtils.copyObj(dto, BannerEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, bannerDao);

        CacheKit.invalidRedisCache(MOBILE_BANNER_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        bannerDao.deleteById(id);

        CacheKit.invalidRedisCache(MOBILE_BANNER_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
